package org.nowcoder.leetcode.twoPointers;


/**
 * Title  : 26. Remove Duplicates from Sorted Array
 * Source : https://leetcode.cn/problems/remove-duplicates-from-sorted-array/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-07-06
 */

public class LeetCode_26 {
    public int removeDuplicates(int[] nums) {
        //暴力解法： set去重
        // Set<Integer> set = new TreeSet<>();
        // for (int num : nums) {
        //     set.add(num);
        // }
        // int i = 0;
        // for (Integer num : set) {
        //     nums[i++] = num;
        // }
        // return i;

        //双指针
        int i = 0, k = 1;
        for (; k < nums.length; k++) {
            if (nums[i] < nums[k]) {
                nums[++i] = nums[k];
            }
        }
        return i + 1;
    }
}
